package com.atguigu.daijia.rules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.atguigu.daijia.model.form.rules.FeeRuleRequest;
import com.atguigu.daijia.model.form.rules.FeeRuleRequestForm;
import com.atguigu.daijia.model.vo.rules.FeeRuleResponse;
import com.atguigu.daijia.model.vo.rules.FeeRuleResponseVo;
import com.atguigu.daijia.rules.mapper.FeeRuleMapper;
import com.atguigu.daijia.rules.service.FeeRuleService;
import lombok.extern.slf4j.Slf4j;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class FeeRuleServiceImpl implements FeeRuleService {


    private final KieContainer kieContainer;

    public FeeRuleServiceImpl(KieContainer kieContainer) {
        this.kieContainer = kieContainer;
    }

    @Override
    public FeeRuleResponseVo calculateOrderFee(FeeRuleRequestForm feeRuleRequestForm) {
        FeeRuleRequest feeRuleRequest = BeanUtil.copyProperties(feeRuleRequestForm, FeeRuleRequest.class);
        //里面的date类型要转化为string类型
        //feeRuleRequest.setStartTime(new DateTime(feeRuleRequestForm.getStartTime()).toString("HH:mm:ss"));
        feeRuleRequest.setStartTime(DateUtil.formatTime(feeRuleRequestForm.getStartTime()));

        log.info("传入参数：{}", JSON.toJSONString(feeRuleRequest));
        //开启kie会话
        KieSession kieSession=kieContainer.newKieSession();
        //将要返回的对象设置成全局变量
        FeeRuleResponse feeRuleResponse=new FeeRuleResponse();
        kieSession.setGlobal("feeRuleResponse",feeRuleResponse);
        kieSession.insert(feeRuleRequest);
        kieSession.fireAllRules();
        kieSession.dispose();
        log.info("返回结果：{}", JSON.toJSONString(feeRuleResponse));
        FeeRuleResponseVo feeRuleResponseVo = BeanUtil.copyProperties(feeRuleResponse, FeeRuleResponseVo.class);
        return feeRuleResponseVo;
        //封装传入对象
//        FeeRuleRequest feeRuleRequest = new FeeRuleRequest();
//        feeRuleRequest.setDistance(feeRuleRequestForm.getDistance());
//        feeRuleRequest.setStartTime(new DateTime(feeRuleRequestForm.getStartTime()).toString("HH:mm:ss"));
//        feeRuleRequest.setWaitMinute(feeRuleRequestForm.getWaitMinute());
//        log.info("传入参数：{}", JSON.toJSONString(feeRuleRequest));
//
//        // 开启会话
//        KieSession kieSession = kieContainer.newKieSession();
//
//        //封装返回对象
//        FeeRuleResponse feeRuleResponse = new FeeRuleResponse();
//        kieSession.setGlobal("feeRuleResponse", feeRuleResponse);
//        // 设置订单对象
//        kieSession.insert(feeRuleRequest);
//        // 触发规则
//        kieSession.fireAllRules();
//        // 中止会话
//        kieSession.dispose();
//        log.info("计算结果：{}", JSON.toJSONString(feeRuleResponse));
//
//        //封装返回对象
//        FeeRuleResponseVo feeRuleResponseVo = new FeeRuleResponseVo();
//        BeanUtils.copyProperties(feeRuleResponse, feeRuleResponseVo);
//        return feeRuleResponseVo;
    }
}
